<?php


namespace app\admin\controller;


use app\logic\DataHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;
use app\logic\DeptHelper;

class Role extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function save_role() {
        $params = get_all_params();


        if (!check_params_empty(['title'])) {
            return json([ 'code' => -1, 'msg' => '请填写：角色名称' ]);
        }

        list($menu_ids_new, $menu_titles_new) = MenuHelper::get_menu_names($params['menu_ids']); //MenuHelper::expand_menu_ids($params['menu_ids']);
        $params['menu_ids'] = $menu_ids_new;
        $params['menu_titles'] = $menu_titles_new;

        list($dept_ids_new, $dept_titles_new) = DeptHelper::get_dept_names($params['dept_ids']); //DeptHelper::expand_dept_ids($params['dept_ids']);
        $params['dept_ids'] = $dept_ids_new;
        $params['dept_titles'] = $dept_titles_new;

        if (isset($params['id']) && $params['id'] > 0) {
            //更新
            DataHelper::update('tb_role', "id=".$params['id'], $params);
        } else {
            //新增
            DataHelper::add('tb_role', $params);
        }

        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function edit($id = 0) {
        if ($id > 0) {
            $role = DataHelper::find_by_id('tb_role', $id);
        } else {
            $role = [
                'id' => 0,
                'title' => '',
                'menu_ids' => '',
                'menu_titles' => '',
                'dept_ids' => '',
                'dept_titles' => '',
                'status' => 0
            ];
        }

        return View::fetch('edit', [
            'role' => $role
        ]);
    }

    public function load_menu($role_id) {
        $checked_ids = [];

        if ($role_id > 0) {
            $role = DataHelper::find_by_id('tb_role', $role_id);
            $checked_ids = explode(',', $role['menu_ids']);
        }

        $tree = MenuHelper::load_menu(0, $checked_ids);

        return json([
            'code' => 0,
            'msg' => '成功',
            'tree' => $tree
        ]);
    }

    public function load_dept($role_id) {
        $checked_ids = [];

        if ($role_id > 0) {
            $role = DataHelper::find_by_id('tb_role', $role_id);
            $checked_ids = explode(',', $role['dept_ids']?$role['dept_ids']:'');
        }

        $tree = DeptHelper::load_dept(0, $checked_ids);

        return json([
            'code' => 0,
            'msg' => '成功',
            'tree' => $tree
        ]);
    }

    public function list($title = '', $page = 1, $limit = 20) {
        $query = " status=0 ";

        if (!empty($title)) {
            $query .= " and title like '%$title%' ";
        }


        list($list, $count) = DataHelper::select_page('tb_role', $query, [ 'page' => $page, 'limit' => $limit ]);

        if ($count <= 0) {
            return json([ 'code' => 201, 'msg' => '没有相关数据' ]);
        }

        return json([
            'code' => 0,
            'msg' => '成功',
            'count' => $count,
            'data' => $list
        ]);
    }

    public function del_role() {
        $params = get_all_params();
        $ids = $params['ids'];

        DataHelper::update('tb_role', "id in ($ids)", [
            'status' => -1
        ]);
        return json([ 'code' => 0, 'msg' => '成功' ]);
    }
}